﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.Collections.ObjectModel;
using System.Text;
using System.Configuration;

namespace TractRecord.Administration
{
    public partial class Users : TractRecordBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int i = 1;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList GetAllUsers()
        {
            ArrayList data = new ArrayList();

            MembershipUserCollection users = Membership.GetAllUsers();

            foreach (MembershipUser user in users)
            {
                data.Add(
                    new
                    {
                        UserId = user.ProviderUserKey,
                        UserName = user.UserName,
                        IsApproved = user.IsApproved.ToString().ToLower() ,
                        IsLockedOut = user.IsLockedOut.ToString().ToLower(),
                        IsOnline = user.IsOnline.ToString().ToLower(),
                        LastActivityDate = user.LastActivityDate.ToString("G"),
                        LastLockoutDate = user.LastLockoutDate.ToString("G"),
                        LastLoginDate = user.LastLoginDate.ToString("G"),
                        LastPasswordChangedDate = user.LastPasswordChangedDate.ToString("G"),
                        CreationDate = user.CreationDate.ToString("G"),
                        Comment = user.Comment,
                        Email = user.Email
                    });
            }
            
            return data;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void UnlockUser(string UserId)
        {
            Guid userid = new Guid(UserId);

            Membership.GetUser(userid).UnlockUser();

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void ApproveUser(string UserId)
        {
            Guid userid = new Guid(UserId);

            MembershipUser user = Membership.GetUser(userid);
            user.IsApproved = !user.IsApproved;

            Membership.UpdateUser(user);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void DeleteUser(string UserId)
        {
            Guid userid = new Guid(UserId);

            MembershipUser user = Membership.GetUser(userid);

            string[] roles = System.Web.Security.Roles.GetRolesForUser(user.UserName);

            foreach (string r in roles)
            {
                System.Web.Security.Roles.RemoveUserFromRole(user.UserName, r);
            }
            
            Membership.DeleteUser(user.UserName, false);
        }
   
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void UpdateUser(string UserId, string Email)
        {
            Guid userid = new Guid(UserId);

            MembershipUser user = Membership.GetUser(userid);

            user.Email = Email;

            Membership.UpdateUser(user);
        }

        
    }
}